/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.DoctorLettersTemplateDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorLettersTemplateTBL;

/**
*
* @author Patrick kendzo
* Email: pkendzo@msn.com
*/

@Stateless
public class DoctorLettersTemplateDAO implements DoctorLettersTemplateDAOInt{
	
    @PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em;
    
    public DoctorLettersTemplateDAO()
    {
    	
    }

	@Override
	public void delete(Long id)
	{
		DoctorLettersTemplateTBL doc = em.find(DoctorLettersTemplateTBL.class, id);
        em.remove(doc);
        em.flush();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DoctorLettersTemplateTBL> listDoctorLetterTemplateBy_Typ(String typ)
	{
		try {

            List<DoctorLettersTemplateTBL> resultList = (List<DoctorLettersTemplateTBL>) em.createQuery(
                    "select dl from DoctorLettersTemplateTBL dl where dl.typ = '" + typ + "' order by label").getResultList();
            return resultList;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Doctor Letter template for this typ can be found ", e);
        }
	}

	@Override
	public void persist(DoctorLettersTemplateTBL todo)
	{
		em.persist(todo);
		em.flush();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DoctorLettersTemplateTBL> retrieveAllDoctorLetterTemplate()
	{
		try {

            List<DoctorLettersTemplateTBL> resultList = (List<DoctorLettersTemplateTBL>) em.createQuery(
                    "select dl from DoctorLettersTemplateTBL dl").getResultList();
            return resultList;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Doctor Letter template for this typ can be found ", e);
        }
	}

	@Override
	public DoctorLettersTemplateTBL retrieveDoctorLetterTemplateByID(Long id)
	{
		return (DoctorLettersTemplateTBL) em.find(DoctorLettersTemplateTBL.class, id);
	}

	@Override
	public void update(DoctorLettersTemplateTBL todo)
	{
		em.merge(todo);
		em.flush();
		
	}

}
